package top.lushi78778.iot.mqtt.web.api.v1;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.onsmqtt20200420.models.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.lushi78778.iot.mqtt.configurer.MqttConfigurer;
import top.lushi78778.iot.mqtt.core.MqttClient;
import top.lushi78778.iot.mqtt.core.Result;
import top.lushi78778.iot.mqtt.core.ResultGenerator;

import java.util.List;

/**
 * @ClassName Client
 * @Description 客户端连接查询接口
 * @Author lushi
 * @Date 2021/7/18 10:43
 */
@RestController
@RequestMapping("/api/client")
public class Client {
    /**
     * 调用QuerySessionByClientId根据Client ID查询指定设备的连接信息
     *
     * @param ClientId 需要查询的目标Client ID
     * @return OnlineStatus 设备是否在线，true：在线 false：不在线 RequestId 公共参数
     * @throws Exception 异常
     */
    @ApiOperation(value = "根据Client ID查询指定设备的连接信息")
    @RequestMapping(value = "/session",method = {RequestMethod.POST})
    public Result<Object> QuerySessionByClientId(@ApiParam(value = "需要查询的目标Client ID",required = true)@RequestParam String ClientId) throws Exception {
        MqttConfigurer mqttConfigurer = new MqttConfigurer();
        com.aliyun.onsmqtt20200420.Client client = MqttClient.createClient(mqttConfigurer.getAccesskey(), mqttConfigurer.getSecretkey(), mqttConfigurer.getEndpoint());
        QuerySessionByClientIdRequest querySessionByClientIdRequest = new QuerySessionByClientIdRequest()
                .setClientId(ClientId)
                .setInstanceId(mqttConfigurer.getInstanceid());
        try {
            QuerySessionByClientIdResponse response = client.querySessionByClientId(querySessionByClientIdRequest);
            JSONObject json = new JSONObject(response.toMap());
            String data = String.valueOf(json.getJSONObject("body"));
            return ResultGenerator.genSuccessResult(data);
        }catch (Exception exception){
            return ResultGenerator.genFailResult(exception.getMessage());
        }
    }

    /**
     * 调用BatchQuerySessionByClientIds根据Client ID批量查询客户端在线状态
     *
     * @param ClientId List<String> 是个列表
     * @return OnlineStatusList 客户端状态列表 客户端的Client ID OnlineStatus true：在线 false：不在线 RequestId 公共参数
     * @throws Exception 异常返回失败
     */
    @ApiOperation(value = "根据 Client ID 批量查询客户端在线状态")
    @RequestMapping(value = "/querysession",method = {RequestMethod.POST})
    public Result<Object> BatchQuerySessionByClientIds(@ApiParam(value = "需要查询的目标Client ID列表，单次最多批量查询10个Client ID的状态",required = true)@RequestParam List<String> ClientId) throws Exception {
        if (ClientId.size()>10){
            return ResultGenerator.genFailResult("单次最多批量查询10个Client ID的状态");
        }
        MqttConfigurer mqttConfigurer = new MqttConfigurer();
        com.aliyun.onsmqtt20200420.Client client = MqttClient.createClient(mqttConfigurer.getAccesskey(), mqttConfigurer.getSecretkey(), mqttConfigurer.getEndpoint());
        BatchQuerySessionByClientIdsRequest batchQuerySessionByClientIdsRequest = new BatchQuerySessionByClientIdsRequest()
                .setInstanceId(mqttConfigurer.getInstanceid())
                .setClientIdList(ClientId);
        try {
            BatchQuerySessionByClientIdsResponse response = client.batchQuerySessionByClientIds(batchQuerySessionByClientIdsRequest);
            JSONObject json = new JSONObject(response.toMap());
            String data = String.valueOf(json.getJSONObject("body"));
            return ResultGenerator.genSuccessResult(data);
        }catch (Exception exception){
            return ResultGenerator.genFailResult(exception.getMessage());
        }
    }

}
